<template>
  <Form ref="formCom" :validation-schema="mySchems" v-slot="{errors}" class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field :class="{err:errors.account}" v-model="form.account" name="account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div v-if="errors.account" class="error">{{errors.account}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field :class="{err:errors.mobile}" v-model="form.mobile" name="mobile"  class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div v-if="errors.mobile" class="error">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field v-model="form.code" :class="{err:errors.code}" name="code" class="input" type="text" placeholder="请输入验证码" />
         <span @click="send" class="code">
          {{time===0?'发送验证码':`${time}秒后发送`}}
        </span>
        <div v-if="errors.code" class="error">{{errors.code}}</div>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model="form.password" :class="{err:errors.password}" name="password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div v-if="errors.password" class="error">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model="form.rePassword" :class="{err:errors.rePassword}" name="rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div v-if="errors.rePassword" class="error">{{errors.rePassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import { reactive } from 'vue-demi'
import schems from '@/utils/vee-validate-schema'
import { userQQPatchCode, userLogin } from '@/api/user'
import { ref } from 'vue'
import Message from '@/components/library/Message'
import { useIntervalFn } from '@vueuse/core'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const time = ref(0)
    const formCom = ref(null)
    const store = useStore()
    const router = useRouter()

    const form = reactive({
      account: null,
      mobile: null,
      code: null,
      password: null,
      rePassword: null
    })
    const mySchems = {
      account: schems.accountApi,
      mobile: schems.mobile,
      password: schems.password,
      rePassword: schems.rePassword,
      code: schems.code
    }

    // 发送验证码
    const send = async () => {
      const valid = mySchems.mobile(form.mobile)
      if (valid === true) {
        if (time.value === 0) {
          await userQQPatchCode(form.mobile)
          Message({ text: '验证码发送成功' })
          form.code = 123456
          time.value = 60
          resume()
        }
      } else {
        formCom.value.setFieldError('mobile', valid)
      }
    }
    // 验证码倒计时
    const { pause, resume } = useIntervalFn(
      () => { time.value--; if (time.value === 0) pause() },
      1000,
      false
    )

    // 立即绑定
    const submit = () => {
      const valid = formCom.value.validate() // 校验整个表单
      if (valid) {
        userLogin({ unionId: props.unionId, ...form }).then(data => {
          const { id, nickname, avatar, token, mobile } = data.result
          store.commit('user/setUser', { id, nickname, avatar, token, mobile })
          store.dispatch('cart/margeCart').then(() => {
            router.push(store.state.user.redirectUrl || '/') // 路由回转
            Message({ text: '登录成功！' })
          })
        }).catch(e => {
          Message({ type: 'error', text: '完善信息失败' })
        })
      }
    }

    return { form, mySchems, send, formCom, time, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
